package com.victor.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.CommentsPhoto;
import com.victor.entity.Personaltravel;
import com.victor.entity.TravelComments;
import com.victor.service.TravelService;
import com.victor.utils.Msg;
import com.victor.utils.OSSUtil;

@RestController
@RequestMapping("/travel")
public class TravelController {
	@Autowired
	private TravelService travelService;
	@Autowired
	private LogController log;
	
	/**
	 * 游记列表
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/findall")
	public Msg getAll(int page,int row,String adminid) {
		if (adminid == null || adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		PageInfo<Personaltravel> list = travelService.selAll(page,row);
		log.addLog(adminid, "查看了游记列表");
		return Msg.success().add("list", list);
	}
	/**
	 * 游记详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/xiangqing")
	public Msg findXiangQing(Integer id,String adminid) {
		if (id == null || adminid.isEmpty() ) {
			return Msg.fail("入参失败");
		}
		Personaltravel personaltravel = travelService.selPersontravelById(id);
		log.addLog(adminid, "查看了"+id+"的游记详情");
		return Msg.success().add("personaltravel", personaltravel);
	}
	/**
	 * 搜索
	 * @param name
	 * @return
	 */
	@RequestMapping("/search")
	public Msg search(String name,int page,int row,String adminid) {
		if (name == null || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<Personaltravel> personaltravel = travelService.selPersontravelByName(name,page,row);
		log.addLog(adminid, "根据"+name+"搜索了游记");
		return Msg.success().add("personaltravel", personaltravel);
		
	}
	
	/**
	 * 删除游记
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delpersontravel")
	public Msg deletePersontravel(int[] ids,String adminid) {
		if (ids == null || ids.length<=0 || adminid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		for (int i = 0; i < ids.length; i++) {
			List<TravelComments> listComments = travelService.selCommentsphotoByTravelId(ids[i]);
			if (listComments != null && listComments.size()>0) {
				for (int j = 0; j < listComments.size(); j++) {
					if (listComments != null && listComments.size()>0) {
						for (int k = 0; k < listComments.get(j).getCommentsPhotos().size(); k++) {
							OSSUtil.deleteFile(listComments.get(j).getCommentsPhotos().get(k).getCphoto());
						}
					}
					
				}
			}
		}
		boolean b = travelService.delPersontravel(ids);
		log.addLog(adminid, "删除了游记"+ids);
		return Msg.judge(b);
	}
	/**
	 * 删除评论
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delcomments")
	public Msg deltetcomments(int[] ids,String adminid) {
		if (ids == null  || ids.length<=0|| adminid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		for (int i = 0; i < ids.length; i++) {
			List<CommentsPhoto> list = travelService.selCommentsphotoByTcId(ids[i]);
			for (int j = 0; j < list.size(); j++) {
				OSSUtil.deleteFile(list.get(j).getCphoto());
			}
		}
		boolean b = travelService.delComments(ids);
		log.addLog(adminid, "删除了游记评论"+ids);
		return Msg.judge(b);
	}
	
	/**
	 * 导出各人游记
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/export")
	public void export(HttpServletRequest request,HttpServletResponse response,String adminid) throws Exception {
		List<Personaltravel> list = travelService.selAllNoPage();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("各人游记列表");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short)0, (short)6);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("各人游记列表");
		
		Row headrow = sheet.createRow(3);
		headrow.createCell(0).setCellValue("用户名");
		headrow.createCell(1).setCellValue("时间");
		headrow.createCell(2).setCellValue("名称");
		headrow.createCell(3).setCellValue("标题");
		headrow.createCell(4).setCellValue("内容");
		headrow.createCell(5).setCellValue("位置");
		headrow.createCell(6).setCellValue("点赞数");
		
		for(Personaltravel personaltravel : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum()+1);
			if(personaltravel.getUsername()== null) {
				dataRow.createCell(0).setCellValue("");
			}else {
				dataRow.createCell(0).setCellValue(personaltravel.getUsername());
			}
			
			if(personaltravel.getTime() == null) {
				dataRow.createCell(1).setCellValue("");
			}else {
				dataRow.createCell(1).setCellValue(personaltravel.getTime());
			}
			if(personaltravel.getAudio() == null) {
				dataRow.createCell(2).setCellValue("");
			}else {
				dataRow.createCell(2).setCellValue(personaltravel.getAudio());
			}
			if(personaltravel.getTitle()== null) {
				dataRow.createCell(3).setCellValue("");
			}else {
				dataRow.createCell(3).setCellValue(personaltravel.getTitle());
			}
			
			if(personaltravel.getContent()== null) {
				dataRow.createCell(4).setCellValue("");
			}else {
				dataRow.createCell(4).setCellValue(personaltravel.getContent());
			}
			if(personaltravel.getWeizhi()== null) {
				dataRow.createCell(5).setCellValue("");
			}else {
				dataRow.createCell(5).setCellValue(personaltravel.getWeizhi());
			}
			if(personaltravel.getDianzan()== null) {
				dataRow.createCell(6).setCellValue("");
			}else {
				dataRow.createCell(6).setCellValue(personaltravel.getDianzan());
			}
		}
		String filename = "各人游记列表";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
		log.addLog(adminid, "导出了游记列表");
	}
	/**
	 * 搜索时间
	 * @param start
	 * @param end
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/searchtime")
	public Msg searchTime(String start,String end,int page,int row,String adminid) {
		if (start ==null && end == null ) {
			return Msg.fail("入参失败");
		}
		PageInfo<Personaltravel> list = travelService.selByTime(start,end,page,row);
		log.addLog(adminid, "根据时间搜索了游记列表");
		return Msg.success().add("list", list);
		
	}

}
